<template>
  <div class="app-container">
    <Ht-search-form
      :dictMap="dictMapAsync"
      :searchExtend="extendSearch"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :advOptions="advOptions"
      permission="user:userInfo:view"
    ></Ht-search-form>

    <ht-table
      ref="dataTable"
      :multi-select="true"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :extendColumns="extendColumns"
      :reqConfig="reqConfig"
      :opButtonList="opButtonList"
      :uploadServiceName="'authorityFileService'"
    >
    </ht-table>

    <add
      ref="userEditForm"
      :title="title"
      center
      :deptParams="deptParams"
      :extendParams="extendColumns"
      @changeTypeInfoOpen="changeTypeInfoOpen"
    ></add>

    <!-- 修改密码配置对话框 -->
    <div v-if="open1">
      <reset-pwd
        :id="curUserId"
        :flag="flag"
        :title="title"
        :open1="open1"
        @changeTypeInfoOpen="changeTypeInfoOpen1"
      ></reset-pwd>
    </div>

    <!-- 委任配置对话框 -->
    <div v-if="open2">
      <add-appoint
        :id="curUserId"
        :title="title"
        :open2="open2"
        :deptId2="deptId2"
        :form2="form2"
        :deptParams="deptParams"
        @changeTypeInfoOpen="changeTypeInfoOpen2"
      ></add-appoint>
    </div>

  </div>
</template>

<script>
import { apiUrlConfig, delUser } from "@/api/authority_app/user/userInfo";
import { userRolesByUserId } from "@/api/authority_app/role/roleInfo";
import add from "@/views/system/userInfo/add";
import resetPwd from "@/views/system/userInfo/resetPwd";
import addAppoint from "@/views/system/userDeptRole/addAppoint";
import { listPageMixin } from "@/libs/listPageMixin";
import { baseUrlConfig } from '@/api/authority_app/base'

export default {
  name: "User",
  mixins: [listPageMixin],
  components: {
    add,
    resetPwd,
    addAppoint,
  },
  data() {
    return {
      extendParentKey: "1",
      opButtonList: () => {
        return [
          {
            label: `${ this.lang('common.add') }`,
            type: "primary",
            icon: "el-icon-plus",
            permission: "user:userInfo:add",
            clickFun: () => {
              this.handleAdd();
            },
          },
          {
            label: `${ this.lang('common.delete') }`,
            type: "danger",
            icon: "el-icon-delete",
            permission: "user:userInfo:remove",
            actionType: 'del',
          },
          // {
          //   label: `${ this.lang('common.import') }`,
          //   type: "success",
          //   icon: "el-icon-upload",
          //   actionType: 'import',
          // },
          // {
          //   label: "导出",
          //   type: "warning",
          //   icon: "el-icon-download",
          //   permission: "user:userInfo:export",
          //   clickFun: () => {
          //     this.handleExport();
          //   },
          // },
        ];
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      // open: false,
      open1: false,
      open2: false,
      // 状态数据字典
      statusOptions: [],
      //选中机构id
      deptId: [],
      deptId1: [],
      deptId2: [],
      disabled: false,
      disabled1: 1,
      // 表单参数
      form: {},
      form2: {},
      // loginName: [],
      formOptions: [],
      advOptions: [],
      // 查询参数
      queryParams: {
        id: "",
        loginName: "",
        userName: "",
        phoneNumber: "",
        status: "",
        deptCodes: "",
        beginTime: "",
        endTime: "",
      },
      reqConfig:apiUrlConfig,
      // 机构参数
      deptParams: {
        userId: "",
        deptId: "",
        deptCode: "",
        tenantCode: "",
        name: "",
      },
      // 列信息
      tableBindColumns: [
        { label: `${ this.lang('authority.userInfo.form.userId') }`, prop: "id", visible: false },
        { label: `${ this.lang('authority.userInfo.form.loginName') }`, prop: "loginName" },
        { label: `${ this.lang('authority.userInfo.form.userName') }`, prop: "userName" },
        { label: `${ this.lang('authority.userInfo.form.tenantName') }`, prop: "tenant.tenantName", sortable: false },
        { label: `${ this.lang('authority.userInfo.form.deptName') }`, prop: "dept.deptName", sortable: false },
        {
          label: `${ this.lang('authority.userInfo.form.status') }`,
          prop: "status",
          visible: true,
          dictCode: "sys_normal_disable",
        },
        {
          label: `${ this.lang('authority.userInfo.form.createTime') }`,
          prop: "createTime",
          visible: true,
          width: "160",
        },
        {
          label: `${ this.lang('common.operate') }`, type: 'operate', buttonList: (row) => [
                {
                  label: `${ this.lang('common.edit') }`,
                  permission: 'user:userInfo:edit',
                  doFunc: (rowData) => {
                    this.handleUpdate(rowData);
                  },
                },
                {
                  label: `${ this.lang('common.appoint') }`,
                  permission: 'user:userInfo:addAppoint',
                  visible: row.userType === 1,
                  doFunc: (rowData) => {
                    this.handleAppoint(rowData);
                  },
                },
                {
                  label: `${ this.lang('common.delete') }`,
                  permission: 'user:userInfo:remove',
                  visible: row.id !== 1,
                  doFunc: (rowData) => {
                    this.handleDelete(rowData);
                  },
                },
                {
                  label: `${ this.lang('common.resetPwd') }`,
                  permission: 'user:userInfo:resetPwd',
                  doFunc: (rowData) => {
                    this.handleResetPwd(rowData);
                  },
                },
          ]
        }
      ],
      curUserId: "",
      flag: false,
      searchFormDictList: ["sys_normal_disable"],
      dictMapAsync: {},
    };
  },
  created() {
    this.$set(this.reqConfig,'uploadUrl',baseUrlConfig.uploadUrlForUser)
    this.getDictsList(["sys_normal_disable"]).then((res) => {
      this.statusOptions = res.result.sys_normal_disable;
    });
  },
  async mounted() {
    const formOptions = [
      {
        type: 'search-dept-tree',
        prop: 'deptCodes',
        defaultAttr: {
          radio: false,
          linkParentChoose: false,
          label: `${ this.lang('authority.userInfo.form.deptName') }`,
          placeholder: `${ this.lang('authority.userInfo.rules.deptEmpty') }`,
        },
      },
      {
        type: "input",
        prop: "loginName",
        defaultAttr: {
          label: `${ this.lang('authority.userInfo.form.loginName') }`,
          placeholder: `${ this.lang('authority.userInfo.rules.loginNameEmpty') }`,
        },
      },
      {
        type: "input",
        prop: "phoneNumber",
        defaultAttr: {
          label:  `${ this.lang('authority.userInfo.form.phoneNumber') }`,
          placeholder: `${ this.lang('authority.userInfo.rules.phoneNumberEmpty') }`,
        },
      },
    ];
    const advOptions = [
      {
        type: "dict",
        dict: "sys_normal_disable",
        prop: "status",
        defaultAttr: {
          label: `${ this.lang('authority.userInfo.form.status') }`,
          placeholder: `${ this.lang('authority.userInfo.rules.statusEmpty') }`,
        },
      },
      {
        type: "dates",
        nameObj: {
          startTimeName: "beginTime",
          endTimeName: "endTime",
        },
        defaultAttr: {
          label: `${ this.lang('authority.userInfo.form.createTime') }`,
          placeholder: ["请输入开始日期", "请输入结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyy-MM-dd",
        },
      },
    ];
    this.formOptions = formOptions;
    this.advOptions = advOptions;
    this.dictMapAsync = this.getDictMap();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.doQuery()
    },
    reset2() {
      this.form2 = {
        id: "",
        userName: "",
        deptId: "",
        roleIds: [],
      };
      this.resetForm("form2");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.beginTime = "";
      this.queryParams.endTime = "";
      this.queryParams.deptIds = "";
      this.resetForm("queryForm");
      // this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.userEditForm.show();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.userEditForm.show(row.id);
    },
    /** 委任按钮操作 */
    handleAppoint(row) {
      this.reset2();
      this.open2 = true;
      this.title = "委任管理";
      this.curUserId = row.id;
      this.form2.userId = row.id;
      this.form2.userName = row.userName;
      this.form2.tenantCode = row.tenantCode;
      this.form2.loginName = row.loginName;
      this.form2.oldDeptId = row.deptId;
      this.deptParams.deptCode = row.deptCode;
      this.deptParams.tenantCode = row.tenantCode;
      this.deptId2 = [row.deptId];
      console.log("row",row)
      userRolesByUserId(row.id, row.deptId).then((response) => {
        response.result.forEach((element) => {
          this.form2.roleIds.push(element.id);
        });
      });
    },

    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.open1 = true;
      this.title = "修改密码";
      this.curUserId = row.id;
      this.flag = row.flag;
      console.log(this.flag);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids.join(",");
      console.log("this.ids",this.ids)
      // const loginName = row.loginName || this.loginName.join(",");
      this.$confirm(
        `是否确认删除 ${row ? "这" : ids.split(",").length}个登录用户吗?`,
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUser(ids);
        })
        .then(() => {
          this.doQuery();
          this.msgSuccess("操作成功，待审核");
        })
        .catch((action) => {});
    },
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm("是否确认导出所有用户操作日志?", "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(function () {
    //       return listUser(queryParams, true);
    //     })
    //     .then((res) => {
    //       console.log(res);
    //       this.downloadFile(res.fileId);
    //     });
    // },
    //一级弹窗状态修改
    changeTypeInfoOpen(open) {
      // this.open = open;
      if (!this.open) {
        this.getList();
      }
    },
    changeTypeInfoOpen1(open) {
      this.open1 = open;
      if (!this.open1) {
        this.getList();
      }
    },
    changeTypeInfoOpen2(open) {
      this.open2 = open;
      if (!this.open2) {
        this.getList();
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container-topSerch {
  min-height: 140px;
  max-height: 300px;
  overflow-y: auto;
}
.app-container-table {
  height: calc(100% - 180px);
  .table-operate {
    height: 32px;
  }
  // .table-content {
  //   height: calc(100% - 32px);
  // }
}
.app-container-footer {
  height: 60px;
}
</style>
